package top.fzqblog.kotlinapp.util

import android.database.Cursor
import top.fzqblog.kotlinapp.func.log

/**
 * Created by 抽离 on 2017/11/21 0021.
 */
/**
 * 使用原生的SqlLiteOpenHelper
 */
//                val dbManager = DBManager.getInstance()
//                val sql = "insert into info(key, value) values('unionId', '134124123412341')"
//                dbManager.execSQL(sql)
//                dbManager.query()
class DBManager {

    private var dbHelper: DBHelper = DBHelper("info", "kotlinapp", 1)

    companion object {

        var dbManager: DBManager? = null

        fun getInstance(): DBManager{
            if (dbManager == null){
                synchronized(DBManager::class){
                    if (dbManager == null){
                        dbManager = DBManager()
                    }
                }
            }
            return dbManager!!
        }

    }

    fun query(){
        val c: Cursor = queryTheCursor()
        while (c.moveToNext()){
            val unionid = c.getString(c.getColumnIndex("value"))
            log("unionId=================$unionid")
        }
    }

    fun queryTheCursor(): Cursor {
        val db = dbHelper.writableDatabase
        log("DBManager --> queryTheCursor")
        return db.rawQuery("SELECT value FROM info where key = 'unionId' ",
                null)
    }


    /**
     * 执行sql语句
     */
     fun execSQL(sql: String) {
        log("sql=====$sql")
        //获取写数据库
        val db = dbHelper.writableDatabase
        //直接执行sql语句
        db.execSQL(sql)//或者
        //关闭数据库
        db.close()
    }



}
